package com.violet.detective.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.violet.detective.entity.Workorder;
import com.violet.detective.search.WorkorderSearchParameter;
import com.violet.detective.service.WorkorderService;
import com.violet.detective.util.ERCode;
import com.violet.detective.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/workOrder")
public class WorkorderController {

    @Resource
    private WorkorderService workorderService;

    @PostMapping("/add")
    public R addWorkOrder(@RequestBody Workorder workorder) {
        boolean result = workorderService.addWorkOrder(workorder);
        return result ? R.ok() : R.error();
    }

    @PostMapping("/update")
    public R updateWorkOrder(@RequestBody Workorder workorder) {
        boolean result = workorderService.editWorkOrder(workorder);
        return result ? R.ok() : R.error();
    }

    @DeleteMapping("/deleteWorkOrder/{id}")
    public R deleteWorkOrder(@PathVariable Integer id) {
        boolean isSuccess = workorderService.deleteWorkOrder(id);
        return isSuccess ? R.ok() : R.error();
    }

    @GetMapping("/getWorkOrderById/{id}")
    public R getWorkOrderById(@PathVariable Integer id) {
        return R.ok(workorderService.getWorkOrderById(id));
    }

    @PostMapping("/search")
    public R searchWorkOrders(@RequestBody WorkorderSearchParameter searchParameter) {
        List<Workorder> result = workorderService.searchWorkOrders(searchParameter);
        return R.ok(result);
    }

    @PostMapping("/page-search")
    public R searchWorkOrders(
            @RequestBody WorkorderSearchParameter searchParameter,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Workorder> resultPage = workorderService.searchWorkOrders(searchParameter, page, size);
        return R.ok(resultPage);
    }

    @GetMapping("/workOrders")
    public R findAllWorkOrders() {
        List<Workorder> result = workorderService.findAllWorkOrders();
        return R.ok(result);
    }

    @GetMapping("/page-workOrders")
    public R findAllWorkOrders(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Workorder> resultPage = workorderService.findAllWorkOrders(page, size);
        return R.ok(resultPage);
    }

}
